# 文件读写
# open

f1 = open("data/theZenOfPython.txt", mode='r', encoding="utf8")
print(f1.read(10))  # 读取10个字符
print(f1.readline())  # 读取一行
print(f1.readline())  # 读取一行
print(f1.readline())  # 读取一行
# 将所有的行读取出来 并构成list
# 如果文件太大则不合适
print(f1.readlines())

# 用完记得关闭
f1.close()

# 写文件
# w 表示写文件 覆盖写
# a 表示追加写
f2 = open("data/write1.txt", mode='a')
# 只能write str类型的数据
f2.write("1")
f2.write("\n")
f2.write("2\n")
f2.write("3\n")
f2.writelines(["a\n", "b\n", "c\n"])

# 用完记得关闭
f2.close()

# 每次读写文件都要调用close 太麻烦了
# with open 会在用完自动关闭文件
with open("data/theZenOfPython.txt") as f1:
    with open("data/write2.txt", mode="w") as f2:
        f2.writelines(f1.readlines())
